package com.cloudbroker.bcs.platform.ltb.middle.extservice.itn.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.cloudbroker.bcs.platform.ltb.middle.extservice.itn.token.ITNTokenFactory;
import com.cloudbroker.bcs.platform.ltb.common.util.HttpsUtil;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.configure.ConfigUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.common.stock.result.StockListData;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.common.stock.result.StockListResult;
import com.cloudbroker.bcs.platform.ltb.common.util.HttpUtil;
import com.cloudbroker.bcs.platform.ltb.middle.extservice.itn.ITNField;
import com.cloudbroker.bcs.platform.ltb.middle.extservice.itn.service.ITNService;

@Service
public class ITNServiceImpl implements ITNService {

	@Override
	public JSONObject queryStockList(Map<String, Object> param) {
		String url = ConfigUtil.get("func.itn.stock.sort.url");
		url = url == null ? "https://open.hs.net/quote/v1/sort" : url;
		String wizard = HttpUtil.doGet(url,
				param, null, ITNField.BEARER
						+ ITNTokenFactory.getInstance().getToken());
		JSONObject jsonObject = JSONObject.parseObject(wizard);
		try {
			return jsonObject.getJSONObject("data").getJSONObject("sort");
		} catch (Exception e) {
			e.printStackTrace();
			return new JSONObject();
		}
	}
	
	@Override
	public StockListResult queryStockListWizard(Map<String, Object> param) {
	    String url = ConfigUtil.get("func.itn.stock.wizard.url" , "https://open.hscloud.cn/quote/v1/wizard");
	    String wizard = HttpsUtil.doGet(url,
	            param, null, ITNField.BEARER
	            + ITNTokenFactory.getInstance().getToken());
	    StockListResult listResult = new StockListResult();
	    JSONObject jsonObject = JSONObject.parseObject(wizard);
	    if(jsonObject.get("error_no") != null){
	        listResult.newDataList();
	    }else{
	        JSONArray json = jsonObject.getJSONArray("data");
	        List<StockListData> datas = new ArrayList<StockListData>();
	        if(json.size()>0){
	            for(int i=0;i<json.size();i++){
	                JSONObject job_title = json.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
	                StockListData stock = new StockListData();
	                stock.setSecu_code((String) job_title.get("prod_code"));
	                stock.setChi_name((String) job_title.get("prod_name"));
	                datas.add(stock);
	            }
	        }
	        listResult.setData_list(datas);
	    }
	    try {
	        return listResult;
	    } catch (Exception e) {
	        e.printStackTrace();
	        return listResult;
	    }
	}
	
	public static void main(String[] args) {
        String data = "[{'prod_code':'010107.SS','prod_name':'21国债⑺','hq_type_code':'XSHG.D','special_marker':0},{'prod_code':'010213.SS','prod_name':'02国债⒀','hq_type_code':'XSHG.D','special_marker':0}]";
        JSONArray json = JSONArray.parseArray(data);
        if(json.size()>0){
            for(int i=0;i<json.size();i++){
              JSONObject job_title = json.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
              System.out.println(job_title.get("prod_code")+"="+job_title.get("prod_name")) ;  // 得到 每个对象中的属性值
            }
        }
    }
	
}
